<template>
	<view class="container">
		<!-- mo-virtual-scroller 使用示例 -->
		<mo-virtual-scroller 
		ref="scroller" 
		:flex="flex" 
		:list="list" 
		itemKey="key" 
		@scroll="scroll">
			<template #header>
				<view class="title" @click="flex=!flex">{{flex?'标题浮动在顶部':'标题不浮动'}} 点击测试</view>
			</template>
			<template v-slot="{ data }">
				<view class="pannel">
					<view class="scroll-item">{{ data.key }} - {{ data.id }}</view>
				</view>
			</template>
			<template #bottom>
				<view v-if="showLoadMore||finish" class="bottom">{{LoadText}}</view>
			</template>
		</mo-virtual-scroller>
	</view>
</template>

<script>
    import moVirtualScroller from '@/components/mo-virtual-scroller/mo-virtual-scroller.vue'
    import { testApi } from '@/components/mo-virtual-scroller/js/testAPI.js'
	export default {
		components: {
			moVirtualScroller
		},
		data() {
			return {
				flex: true,
				viewHight: 0,
				list:[],
				dataList: [],
				showLoadMore: false,
				LoadText: '正在加载···',
				finish: false,
				size: 15,
				page: 1
			}
		},
		onLoad() {
			this.initData();
		},
		mounted(){
			this.getViewH();
		},
		methods: {
			getViewH(){
				const query = uni.createSelectorQuery().in(this)
				query.select('.container').boundingClientRect(res=>{
					if(!res) return
					this.viewHight = res.height
                }).exec();
			},
			scroll(e){
				if(this.finish) return
				const dt = e&&e.detail ? e.detail.scrollHeight - e.detail.scrollTop + (this.flex ? this.$refs.scroller.headerHeight : 0) : 0
				if(dt&&dt - this.viewHight < 50){
					this.moreList()
				}
			},
			async initData(){
				this.finish = false
				this.page = 1
				await this.getList();
				this.list = this.dataList
				uni.stopPullDownRefresh();
			},
			async moreList(){
				if(this.finish||this.showLoadMore) return
				this.page ++
				await this.getList();
				this.list = this.list.concat(this.dataList)
				uni.stopPullDownRefresh();
			},
			async getList() {
				this.showLoadMore = true;
				this.LoadText = '正在加载···'
				const params = {
					test: '传入的测试数据'
				}
                const res = await testApi(this.page, this.size, params, 42)
				console.log('testApi',res)

                this.dataList = res.data
				this.finish = this.checkFinish(res.data)
				if(this.finish) {
					this.LoadText = '没有更多了'
				}
				this.showLoadMore = false;
            },
			checkFinish(data){
				return (!data.length||data.length<this.size)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		font-size: 28rpx;
		background: #f8f8f8;
		height: calc(100vh - 88rpx);
		height: calc(100vh - 88rpx - env(safe-area-inset-bottom));
		height: calc(100vh - 88rpx - constant(safe-area-inset-bottom));
	}
	.pannel{
		padding: 20rpx 0;
	}
	.scroll-item{
		padding: 30rpx;
		margin: 0 20rpx;
		background: #fff;
		border-radius: 20rpx;
		box-shadow: 0 0 16rpx rgba($color: #000000, $alpha: .05);
	}
	.title{
		background: #fff;
		padding: 30rpx;
		font-size: 32rpx;
		border-bottom: 1rpx solid #eee;
		border-top: 1rpx solid #f8f8f8;
	}
	.bottom{
		text-align: center;
		padding: 20rpx 0 30rpx;
	}
</style>
